package com.lkwlw.tplms.order.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lkwlw.tplms.model.Pager;
import com.lkwlw.tplms.order.dao.mapper.OrderinfoMapper;
import com.lkwlw.tplms.order.dao.model.Orderinfo;
import com.lkwlw.tplms.order.dao.model.OrderinfoExample;
import com.lkwlw.tplms.order.dao.model.OrderinfoExample.Criteria;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class OrderManageViewAction extends ActionSupport {

	private static final long serialVersionUID = 5587525422264118305L;
	private Orderinfo order;
	private OrderinfoMapper orderInfoDAO;
	private List<Orderinfo> orderList;
	private String selectIds;
	private String sendDate = "";	
	private String receiveDate = "";
	private Pager pager;	
	private int page = 1;
	
	public String execute() throws Exception{
//		if(selectIds!=null){
//			return INPUT;
//		}else{
			OrderinfoExample example = new OrderinfoExample();
			Criteria criteria =  example.createCriteria();
			if(null != order){
				if(null != order.getId() && !"".equals(order.getId())){
					criteria.andIdIsNotNull();
					criteria.andIdLike(order.getId());
				}
				if(null != order.getSendertel() && !"".equals(order.getSendertel())){
					criteria.andSendertelIsNotNull();
					criteria.andSendertelLike(order.getSendertel());
				}
				if(null != order.getSender() && !"".equals(order.getSender())){
					criteria.andSenderIsNotNull();
					criteria.andSenderLike(order.getSender());
				}			
				if(null != order.getState() && !"".equals(order.getState())){
					criteria.andStateIsNotNull();
					criteria.andStateEqualTo(order.getState());
				}else{
					criteria.andStateNotEqualTo("未收件").andStateNotEqualTo("已取消");
				}
			}
			example.setOrderByClause("id asc");
			getPagination(example);
			return SUCCESS;
		//}
	}
	
	public String query(){
			OrderinfoExample example = new OrderinfoExample();
			Criteria criteria =  example.createCriteria();
			if(null != order){
				if(null != order.getId() && !"".equals(order.getId())){
					criteria.andIdIsNotNull();
					criteria.andIdLike(order.getId());
				}
				if(null != order.getSendertel() && !"".equals(order.getSendertel())){
					criteria.andSendertelIsNotNull();
					criteria.andSendertelLike(order.getSendertel());
				}
				if(null != order.getSender() && !"".equals(order.getSender())){
					criteria.andSenderIsNotNull();
					criteria.andSenderLike(order.getSender());
				}			
				if(null != order.getState() && !"".equals(order.getState())){
					criteria.andStateIsNotNull();
					criteria.andStateEqualTo(order.getState());
				}else{
					criteria.andStateNotEqualTo("未收件").andStateNotEqualTo("已取消");
				}
			}
			if(selectIds!=null){
				order = orderInfoDAO.selectByPrimaryKey(selectIds);
				orderList= orderInfoDAO.selectByOrderId(selectIds);
				Map session = ActionContext.getContext().getSession();
				session.remove("order");
				if(orderList.size()==0){					
					session.put("order", 0);
				}
				return INPUT;
			}else{
				//orderList = orderInfoDAO.selectByExample(example);
				getPagination(example);
				order.setId("");
				order.setSender("");
				order.setSendertel("");
				return SUCCESS;
			}
	}
	
	public String edit() throws Exception {
		
		return SUCCESS;
	}
	
	public String save() throws Exception {
		if (order.getState().equals("未收件")) {
			order.setState("已收件");
		}
		orderInfoDAO.updateByPrimaryKeySelective(order);
		//query();
		orderList = orderInfoDAO.selectByExample(null,new OrderinfoExample());
		selectIds = "";
		return SUCCESS;
	}
	
	public String delete() throws Exception {
		
		return SUCCESS;
	}
	private void getPagination(OrderinfoExample example){
		int pageSize=10;
		pager.setCurrentPage(page);
		pager.setPageSize(pageSize);
		
		// 获得总记录数
		int totalSize = orderInfoDAO.countByExample(example);
		pager.setTotalCount(totalSize);
		Map<String, Integer> limitParam = new HashMap<String, Integer>();
		limitParam.put("offset", pager.getStart());
		limitParam.put("rows", pager.getPageSize());
		
		orderList = null;		
		orderList = orderInfoDAO.selectByExample(limitParam,example);
		pager.setItems(orderList);
		pager.setPaginationModel(getPagerStr(totalSize));		
	}
	
	/**
	 * 组织分页导航栏
	 * @return
	 */
	public String  getPagerStr(int totalSize) {
		
		StringBuilder sb=new StringBuilder();
		
		int pageCount=pager.getTotalPage();//总页数
		int a_total=10;      //分页条中有多少个超链接
		
		sb.append("<table><tr><td align='center'class='wordblue'>共" + pager.getTotalCount()+"条数据&nbsp;&nbsp;页次");
		sb.append(pager.getCurrentPage()+"/" + (totalSize == 0 ? 1 : pageCount) + "页&nbsp;&nbsp;</td>");
		//实际应用中修改上面第三个参数即可

		int a_padding=(int)Math.ceil(a_total/2);  //中间的那个超链接距离边缘链接的间隔a的个数 例如：共11个分页 那么这个就是5

		if(pageCount - pager.getCurrentPage() <= a_padding && pager.getCurrentPage() > a_padding + 1) {
			a_padding=a_total-(pageCount-pager.getCurrentPage());
		}

		int start=pager.getCurrentPage()-a_padding,
		end=start+a_total;
		

		if(pager.getCurrentPage() > 1){
			sb.append("<td class='wordblue'><a href=order/orderManageViewQuery?page=1>首页</a>&nbsp;&nbsp;</td>");
			sb.append("<td ><div class='prepage wordblue'><a href=order/orderManageViewQuery?page="+(pager.getCurrentPage()-1)+">上一页</a></div></td>");
		}
		sb.append("<td ><div class='pagelist'>");
		if (totalSize > 0) {
			for (int i = start; i <=end; i++) {
				if(i<=0){
					end+=Math.abs(i);
					i=1;	
				}
				sb.append(" <a href=order/orderManageViewQuery?page=" + i + " " + ( i == pager.getCurrentPage() ? "style=\"color:red;\"":"") + "> " + i + " </a>");	
				if(i==pageCount){
					break;
				}
			}
		}		
		sb.append("</div></td>");
		if(pager.getCurrentPage()-pageCount<0){
			sb.append("<td><div class='nextpage wordblue'>");
			sb.append("<a href=order/orderManageViewQuery?page=" + ( pager.getCurrentPage() + 1 ) + ">下一页</a></div></td>");
			sb.append("<td><div class='wordblue'><a href=order/orderManageViewQuery?page=" + pageCount + ">尾页</a></div></td>");
			}
		sb.append("</tr></table>");
		return sb.toString();
	}
	
	public Orderinfo getOrder() {
		return order;
	}

	public void setOrder(Orderinfo order) {
		this.order = order;
	}

	public OrderinfoMapper getOrderInfoDAO() {
		return orderInfoDAO;
	}

	public void setOrderInfoDAO(OrderinfoMapper orderInfoDAO) {
		this.orderInfoDAO = orderInfoDAO;
	}

	public List<Orderinfo> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<Orderinfo> orderList) {
		this.orderList = orderList;
	}

	public String getSelectIds() {
		return selectIds;
	}

	public void setSelectIds(String selectIds) {
		this.selectIds = selectIds;
	}

	public String getSendDate() {
		return sendDate;
	}

	public void setSendDate(String sendDate) {
		this.sendDate = sendDate;
	}

	public String getReceiveDate() {
		return receiveDate;
	}

	public void setReceiveDate(String receiveDate) {
		this.receiveDate = receiveDate;
	}
	
	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

}
